package com.tjmc.tiomsresource.service.impl;

import com.google.common.collect.Lists;
import com.tjmc.tiomscore.dao.*;
import com.tjmc.tiomscore.model.*;
import com.tjmc.tiomsresource.service.CMBusiService;
import com.tjmc.tiomsresource.utils.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Luc
 * @version 1.0
 * @date 2020-06-03 15:52
 */
@Service("cmBusiService")
public class CMBusiServiceImpl implements CMBusiService {

    private static Logger logger = LoggerFactory.getLogger(CMBusiServiceImpl.class);

    @Autowired
    private TiBusiMapper tiBusiMapper;
    @Autowired
    private TiBusiEsiMapper tiBusiEsiMapper;
    @Autowired
    private TiBusiEspMapper tiBusiEspMapper;
    @Autowired
    private TranPtnBusiEsiMapper tranPtnBusiEsiMapper;
    @Autowired
    private TranPtnBusiEspMapper tranPtnBusiEspMapper;
    @Autowired
    private TiNelMapper tiNelMapper;
    @Autowired
    private TiPortMapper tiPortMapper;

    @Override
    public void buildTiBusiToDB() {
        tiBusiMapper.truncateTable();
        tiBusiMapper.insertDataFromTranPtnBusi();
        logger.info("insertDataFromTranPtnBusi 完成");
        this.buildTiBusiEsiToDB();
        logger.info("buildTiBusiEsiToDB 完成");
        this.buildTiBusiEspToDB();
        logger.info("buildTiBusiEspToDB 完成");
    }

    @Override
    public List<TiBusi> getTiBusisByTiNels(List<TiNel> tiNelList) {
        if(null != tiNelList && tiNelList.size() > 0) {
            List<String> values = new ArrayList<>();
            for(TiNel tiNel : tiNelList) {
                values.add(tiNel.getRmuid());
            }
            if(values.size() > 0) {
                TranPtnBusiEspExample tranPtnBusiEspExample = new TranPtnBusiEspExample();
                tranPtnBusiEspExample.createCriteria().andNermuidIn(values);
                List<TranPtnBusiEsp> tranPtnBusiEspList = tranPtnBusiEspMapper.selectByExample(tranPtnBusiEspExample);
                if(null != tranPtnBusiEspList) {
                    values.clear();
                    for(TranPtnBusiEsp tranPtnBusiEsp : tranPtnBusiEspList) {
                        values.add(tranPtnBusiEsp.getServicermuid());
                    }
                    if(values.size() > 0) {
                        TiBusiExample tiBusiExample = new TiBusiExample();
                        tiBusiExample.createCriteria().andServicermuidIn(values);
                        return tiBusiMapper.selectByExample(tiBusiExample);
                    }
                }
            }
        }
        return null;
    }

    /*
     * @description:  根据网元列表，分类业务
     * @param: [tiNelList]
     * @return: java.util.Map<java.lang.String,java.util.List<com.tjmc.tiomscore.model.TiBusi>>
     * @date: 2020/6/9
     */
    @Override
    public Map<String, List<TiBusi>> classifyTiBusisByTiNels(List<TiNel> tiNelList) {
        if(null != tiNelList && tiNelList.size() > 0) {
            List<TiBusi> tiBusiList = this.getTiBusisByTiNels(tiNelList);
            if(null != tiBusiList && tiBusiList.size() > 0) {
                Map<String, List<TiBusi>> busiMap = new HashMap<>();
                for(TiBusi tiBusi :tiBusiList) {
                    String ownesservicetype = tiBusi.getOwnesservicetype().trim();
                    if(!Constants.BUSI_TYPES.contains(ownesservicetype)) {
                        continue;
                    }
                    if(!busiMap.containsKey(ownesservicetype)) {
                        busiMap.put(ownesservicetype, new ArrayList<>());
                    }
                    busiMap.get(ownesservicetype).add(tiBusi);
                }
                return busiMap.size()>0?busiMap:null;
            }
        }
        return null;
    }

    @Override
    public Map<String, List<TiBusi>> mergeTiBusisMap(List<Map<String, List<TiBusi>>> tiBusisMapList) {
        if(null != tiBusisMapList && tiBusisMapList.size() > 0) {
            Map<String, List<TiBusi>> mergeMap = new HashMap<>();
            for(Map<String, List<TiBusi>> map : tiBusisMapList) {
                for(String key : map.keySet()) {
                    if(!mergeMap.containsKey(key)) {
                        mergeMap.put(key, new ArrayList<>());
                    }
                    mergeMap.get(key).addAll(map.get(key));
                }
            }
            return mergeMap.size()>0?mergeMap:null;
        }
        return null;
    }

    private void buildTiBusiEsiToDB() {
        tiBusiEsiMapper.truncateTable();
        List<TranPtnBusiEsi> tranPtnBusiEsiList = tranPtnBusiEsiMapper.selectByExample(null);
        TiNelExample tiNelExample;
        TiBusiExample tiBusiExample;
        List<TiBusiEsi> tiBusiEsiList = new ArrayList<>();
        for(TranPtnBusiEsi tranPtnBusiEsi :tranPtnBusiEsiList) {
            TiBusiEsi tiBusiEsi = new TiBusiEsi();
            tiBusiExample = new TiBusiExample();
            tiBusiExample.createCriteria().andServicermuidEqualTo(tranPtnBusiEsi.getServicermuid());
            List<TiBusi> tiBusiList = tiBusiMapper.selectByExample(tiBusiExample);
            if(null == tiBusiList || tiBusiList.size() != 1) {
                continue;
            }
            tiBusiEsi.setTiBusiId(tiBusiList.get(0).getId());
            tiBusiEsi.setServicermuid(tranPtnBusiEsi.getServicermuid());
            tiBusiEsi.setPwrmuid(tranPtnBusiEsi.getPwrmuid());
            tiBusiEsi.setTiPswId((long) 0);
            if(null == tranPtnBusiEsi.getAendnermuid()) {
                tiBusiEsi.setTiPswAnelId((long) 0);
            } else {
                tiNelExample = new TiNelExample();
                tiNelExample.createCriteria().andRmuidEqualTo(tranPtnBusiEsi.getAendnermuid());
                List<TiNel> tiNels = tiNelMapper.selectByExample(tiNelExample);
                if(null == tiNels || tiNels.size() != 1) {
                    continue;
                }
                tiBusiEsi.setTiPswAnelId(tiNels.get(0).getId());
            }
            if(null == tranPtnBusiEsi.getZendnermuid()) {
                tiBusiEsi.setTiPswZnelId((long) 0);
            } else {
                tiNelExample = new TiNelExample();
                tiNelExample.createCriteria().andRmuidEqualTo(tranPtnBusiEsi.getZendnermuid());
                List<TiNel> tiNels = tiNelMapper.selectByExample(tiNelExample);
                if(null == tiNels || tiNels.size() != 1) {
                    continue;
                }
                tiBusiEsi.setTiPswZnelId(tiNels.get(0).getId());
            }
            tiBusiEsi.setRole(tranPtnBusiEsi.getRole());
            tiBusiEsi.setTranPtnBusiEsiId(tranPtnBusiEsi.getId());
            tiBusiEsiList.add(tiBusiEsi);
        }
        if(tiBusiEsiList.size() != 0) {
            List<List<TiBusiEsi>> lists = Lists.partition(tiBusiEsiList, Constants.BATCH_SPLIT_SIZE);
            for(List<TiBusiEsi> tiBusiEsis : lists) {
                tiBusiEsiMapper.insertBatch(tiBusiEsis);
            }
        }
    }

    private void buildTiBusiEspToDB() {
        tiBusiEspMapper.truncateTable();
        List<TranPtnBusiEsp> tranPtnBusiEspList = tranPtnBusiEspMapper.selectByExample(null);
        TiNelExample tiNelExample;
        TiBusiExample tiBusiExample;
        TiPortExample tiPortExample;
        List<TiBusiEsp> tiBusiEspList = new ArrayList<>();
        for(TranPtnBusiEsp tranPtnBusiEsp : tranPtnBusiEspList) {
            TiBusiEsp tiBusiEsp = new TiBusiEsp();
            tiBusiEsp.setServicermuid(tranPtnBusiEsp.getServicermuid());
            tiBusiExample = new TiBusiExample();
            tiBusiExample.createCriteria().andServicermuidEqualTo(tranPtnBusiEsp.getServicermuid());
            List<TiBusi> tiBusiList = tiBusiMapper.selectByExample(tiBusiExample);
            if(null == tiBusiList || tiBusiList.size() != 1) {
                continue;
            }
            tiBusiEsp.setTiBusiId(tiBusiList.get(0).getId());
            tiNelExample = new TiNelExample();
            tiNelExample.createCriteria().andRmuidEqualTo(tranPtnBusiEsp.getNermuid());
            List<TiNel> tiNels = tiNelMapper.selectByExample(tiNelExample);
            if(null == tiNels || tiNels.size() != 1) {
                continue;
            }
            tiBusiEsp.setTiNelId(tiNels.get(0).getId());
            tiPortExample = new TiPortExample();
            tiPortExample.createCriteria().andRmuidEqualTo(tranPtnBusiEsp.getPortrmuid());
            List<TiPort> tiPorts = tiPortMapper.selectByExample(tiPortExample);
            if(null == tiPorts || tiPorts.size() != 1) {
                continue;
            }
            tiBusiEsp.setTiPortId(tiPorts.get(0).getId());
            tiBusiEsp.setTranPtnBusiEspId(tranPtnBusiEsp.getId());
            tiBusiEspList.add(tiBusiEsp);
        }
        if(tiBusiEspList.size() != 0) {
            List<List<TiBusiEsp>> lists = Lists.partition(tiBusiEspList, Constants.BATCH_SPLIT_SIZE);
            for(List<TiBusiEsp> tiBusiEsps : lists) {
                tiBusiEspMapper.insertBatch(tiBusiEsps);
            }
        }
    }


}
